package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/order")
@SessionAttributes({"order", "orderList", "account"})
public class OrderController {

    @Autowired
    public OrderService orderService;

    @Autowired
    public CartService cartService;

    private Order order = new Order();
    private boolean shoppingAddressRequired;
    private boolean confirmed;
    private List<Order> orderList;
    private static final List<String> CARD_TYPE_LIST;
    String msg;

    static {
        List<String> cardList = new ArrayList<String>();
        cardList.add("Visa");
        cardList.add("MasterCard");
        cardList.add("American Express");
        CARD_TYPE_LIST = Collections.unmodifiableList(cardList);
    }

    @GetMapping("/listOrders")
    public String listOrders(HttpSession session, Model model) {
        Account account = (Account) session.getAttribute("account");
        orderList = orderService.getOrdersByUsername(account.getUsername());
        return "order/ListOrders";
    }

    @GetMapping("/newOrderForm")
    public String newOrderForm(HttpSession session, Model model) {
        Account account = (Account) session.getAttribute("account");
        Cart cart = (Cart) session.getAttribute("cart");

        if (account != null) {
            if (cart != null) {
                order.initOrder(account, cart);
                model.addAttribute("order", order);
                return "order/newOrderForm";
            } else {
                msg = "An order could not be created because a cart could not be found.";
                model.addAttribute("msg", msg);
                return "commom/Error";
            }
        } else {
            msg = "You must sign on before attempting to check out.  Please sign on and try checking out again.";
            model.addAttribute("msg", msg);
            return "account/signonForm";
        }
    }

    @RequestMapping("/newOrder")
    public String newOrder(HttpServletRequest request, HttpSession session, Boolean shippingAddressRequired, Model model) {

        if(shippingAddressRequired != null)
            shoppingAddressRequired = true;

        if(request.getParameter("confirmed") != null)
            confirmed = (request.getParameter("confirmed").equals("1"));

        if (shoppingAddressRequired) {
            shoppingAddressRequired = false;
            return "order/ShippingForm";
        }
        else if (!isConfirmed()) {
            return "order/ConfirmOrder";
        }
        else if (getOrder() != null) {
            orderService.insertOrder(order);
            Cart cart = (Cart) session.getAttribute("cart");
            Account account = (Account) session.getAttribute("account");

            //清空购物车
            String workingItemId = (String) session.getAttribute("workingItemId");
            cartService.removeAllCartItemsByUserid(account.getUsername());
            session.removeAttribute("cart");
            session.setAttribute("cart",null);
            session.removeAttribute("cartItem");
            session.setAttribute("workingItemId", workingItemId);
            session.setAttribute("order", order);
            msg = "Thank you, your order has been submitted.";
            model.addAttribute("msg", msg);
            return "order/ViewOrder";
        }
        else {
            msg = "An error occurred processing your order (order was null).";
            model.addAttribute("msg", msg);
            return "common/Error";
        }
    }

    @GetMapping("/viewOrder")
    public String viewOrder(HttpSession session, Model model) {
        Account account = new Account();
        order = orderService.getOrder(order.getOrderId());
        if (account.getUsername().equals(order.getUsername())) {
            return "order/ViewOrder";
        } else {
            order = null;
            msg = "You may only view your own orders.";
            return "common/Error";
        }
    }

    public Order getOrder() {
        return order;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

}
